package work.fram;

import com.google.common.base.Joiner;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;

public class KeyValueMapper {

    private Map<Object, Object> kv = new HashMap<>();

    private Map<Object, Object> invariable = new HashMap<>();

    private String keyString;
    private String valueString;

    private String kvString;


    public void clearInvariable() {
        this.invariable.clear();
    }

    //放入不变值
    public void putInvariable(Object key, Object value) {
        if (value instanceof String) {
            value = "\"" + value + "\"";
        }
        this.invariable.put(key, value);
        this.kv.put(key, value);
    }

    public void put(Object key, Object value) {
        if (value instanceof String) {
            value = "\"" + value + "\"";
        }
        this.kv.put(key, value);
    }

    public boolean make(String split, String kvSplit) {

        if (kvSplit == null) {
            keyString = Joiner.on(split).join(kv.keySet());
            valueString = Joiner.on(split).join(kv.values());
        } else {
            this.kvString = Joiner.on(",").withKeyValueSeparator(kvSplit).join(kv);
        }
        return true;
    }

    public String getKeyString() {
        return keyString;
    }

    public String getValueString() {
        return valueString;
    }

    public void reset() {
        this.keyString = null;
        this.valueString = null;
        this.kvString = null;
        this.kv.clear();
        this.kv.putAll(this.invariable);
    }

    public String getKvString() {
        return kvString;
    }

    @Override
    public String toString() {
        return getKvString();
    }
}
